﻿using System;


namespace Sencha.Serialization.Bson.Serializers
{
	public class BsonMinKeySerializer : TypeSerializer<BsonMinKey>
	{
		public override BsonMinKey Deserialize(IJsonReader reader)
		{
			if (reader == null)
				throw new ArgumentNullException("reader");

			if (reader is IBsonReader)
				return this.DeserializeBson(reader as IBsonReader);

			reader.ReadObjectBegin();
			reader.ReadMember<int>("$minkey");
			reader.ReadObjectEnd(false);

			return BsonMinKey.Value;

		}
		private BsonMinKey DeserializeBson(IBsonReader reader)
		{
			if (reader.Token != BsonToken.MinKey)
				throw new Exceptions.UnexpectedBsonToken(reader, BsonToken.MinKey);

			return BsonMinKey.Value;
		}
		public override void Serialize(IJsonWriter writer, BsonMinKey value)
		{
			if (writer == null)
				throw new ArgumentNullException("writer");

			if (value == null)
			{
				writer.WriteNull();
				return;
			}

			if (writer is IBsonWriter)
			{
				this.SerializeBson(writer as IBsonWriter, value);
				return;
			}

			writer.WriteObjectBegin();
			writer.WriteMember<int>("$minkey", 1);
			writer.WriteObjectEnd();
		}
		private void SerializeBson(IBsonWriter writer, BsonMinKey value)
		{
			writer.Write(BsonToken.MinKey, (object)null);
		}
	}
}
